package com.aaa.controller;

import com.aaa.entity.Pager;
import com.aaa.service.*;
import com.alibaba.fastjson.JSON;

import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "ClassServlet",urlPatterns = "/class.action")
public class ClassServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method");
        switch (method) {
            case "query_class":
                query_class(request, response);
                break;
            case "query_by_top":
                query_by_top(request, response);
                break;
            case "select_dept":
                select_dept(request, response);
                break;
            case "select_course":
                select_course(request, response);
                break;
            case "select_class_room":
                select_class_room(request, response);
                break;
            case "add_class_up":
                add_class_up(request, response);
                break;
            case "query_without_class":
                query_without_class(request, response);
                break;
            case "query_class_one":
                query_class_one(request, response);
                break;
            case "update_student_classid":
                update_student_classid(request, response);
                break;
            case "update_class_progressing":
                update_class_progressing(request, response);
                break;
            case "query_class_room_pagers":
                query_class_room_pagers(request, response);
                break;
            case "update_class_room":
                update_class_room(request, response);
                break;
            case "deleteClassRoom":
                deleteClassRoom(request, response);
                break;
            case "test":
                test(request, response);
                break;
            case "checkRoomNameIsExits":
                checkRoomNameIsExits(request, response);
                break;
            case "addClassRoom":
                addClassRoom(request, response);
                break;
            case "queryOneClass":
                queryOneClass(request, response);
                break;
            case "delete_class":
                delete_class(request, response);
                break;

        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    private void query_class(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ClassService c = new ClassServiceImpl();
        List<Map> list = c.query_class();
        String str = JSON.toJSONStringWithDateFormat(list, "yyyy-mm-dd");
        response.getWriter().print(str);
    }

    private void query_by_top(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String pageNo = request.getParameter("pageNo") == null ? "1" : request.getParameter("pageNo");
        String pageSize = request.getParameter("pageSize") == null ? "1" : request.getParameter("pageSize");
        String className = request.getParameter("className");
        String classState = request.getParameter("classState");
        Map<String, String> condation = new HashMap<>();
        condation.put("className", className);
        condation.put("classState", classState);
        ClassService c = new ClassServiceImpl();
        Pager pager = c.query_by_top(Integer.parseInt(pageNo), Integer.valueOf(pageSize), condation);
        String str = JSON.toJSONStringWithDateFormat(pager, "yyyy-mm-dd");
        response.getWriter().print(str);
    }

    private void select_dept(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        DeptService d = new DeptServiceImpl();
        List<Map> map = d.query(0);
        String str = JSON.toJSONString(map);
        response.getWriter().print(str);
    }

    private void select_course(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ClassService c = new ClassServiceImpl();
        List<Map> map = c.query_course();
        String str = JSON.toJSONString(map);
        response.getWriter().print(str);
    }

    private void select_class_room(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ClassService c = new ClassServiceImpl();
        List<Map> map = c.query_class_room();
        String str = JSON.toJSONString(map);
        response.getWriter().print(str);
    }

    private void add_class_up(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ClassService c = new ClassServiceImpl();
        List list = new ArrayList();
        String classname = request.getParameter("classname");
        String dept_name = request.getParameter("select_dept");
        String teacher = request.getParameter("teacher");
        String opentime = request.getParameter("opentime");
        String endtime = request.getParameter("endtime");
        String limitnum = request.getParameter("limitnum");
        String courseid = request.getParameter("select_course");
        String classroom = request.getParameter("select_class_room");
        String classid = request.getParameter("classid");
        list.add(Integer.valueOf(courseid));
        list.add(classname);
        list.add(limitnum);
        list.add(java.sql.Date.valueOf(opentime));
        list.add(java.sql.Date.valueOf(endtime));
        list.add(classroom);
        list.add(teacher);
        list.add(dept_name);
        if(classid!=null){
            list.add(Integer.valueOf(classid));
        }
        PrintWriter writer = response.getWriter();
        int result=-1;
        if(classid==null||classid.equals("")){
             result = c.add_class_up(list);
            if (result != -1) {
                writer.print("ok");
            } else {
                writer.print("error");
            }
        }else{
             result=c.updateOneClass(list);
             if(result != -1){
                 writer.print("ok");
             }else{
                 writer.print("error");
             }
        }

    }

    private void query_without_class(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ClassService c = new ClassServiceImpl();
        List<Map> map = c.query_without_class();
        String str = JSON.toJSONStringWithDateFormat(map, "yyyy-mm-dd");
        response.getWriter().print(str);
    }

    private void query_class_one(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ClassService c = new ClassServiceImpl();
        List<Map> list = c.query_class_one(Integer.parseInt(request.getParameter("classid")));
        Map map = list.get(0);
        String str = JSON.toJSONStringWithDateFormat(map, "yyyy-mm-dd");
        response.getWriter().print(str);
    }

    private void update_student_classid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ClassService c = new ClassServiceImpl();
        String classid = request.getParameter("classid");
        String str = request.getParameter("strIds");
        String[] ids = str.split(",");
        System.out.println(ids);
        int result = c.update_student_classid(ids, Integer.parseInt(classid));
        response.getWriter().print(result);

    }

    private void update_class_progressing(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ClassService c = new ClassServiceImpl();
        String classid = request.getParameter("classid");
        int result = c.update_class_progressing(Integer.parseInt(classid));
        if (result != -1) {
            response.getWriter().print("ok");
        } else {
            response.getWriter().print("error");
        }
    }

    private void query_class_room_pagers(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String curPage = request.getParameter("curPage");
        if (curPage == null) curPage = "1";
        ClassService c = new ClassServiceImpl();
        Pager pager = c.query_class_room_pagers(Integer.parseInt(curPage));
        String tr = JSON.toJSONString(pager);
        response.getWriter().print(tr);
    }

    private void update_class_room(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String roomName = request.getParameter("roomName");
        String room_name = request.getParameter("room_name");
        if (roomName.equals(room_name)) {
            response.getWriter().print("same");
            return;
        } else {
            ClassService c = new ClassServiceImpl();
            int result = -1;
            result = c.update_class_room(roomName, room_name);
            if (result == 1) {
                response.getWriter().print("ok");
            } else if (result == -1) {
                response.getWriter().print("error");
            } else if (result == -2) {
                response.getWriter().print("isExist");
            }
        }
    }

    private void deleteClassRoom(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String roomName = request.getParameter("roomName");
        ClassService c = new ClassServiceImpl();
        int result = -1;
        result = c.deleteClassRoom(roomName);
        PrintWriter writer = response.getWriter();
        if (result == 1) {
            writer.print("ok");
        } else {
            writer.print("error");
        }

    }

    private void test(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String jsonResult = "";
        if (username != null) {
            //服务层service调用数据库访问层dao中的searchUserName方法。
            ClassService c = new ClassServiceImpl();
            List<Map> map = c.selectClassRoom(username);

            boolean b;
            if (!map.isEmpty()) {
                b = true;
            } else {
                b = false;
            }
            if (b) {
                //如果名称存在
                jsonResult = "{\"valid\":false}";
            } else {
                //如果该名称不存在
                jsonResult = "{\"valid\":true}";
            }
        } else {
            //如果该名称不存在
            jsonResult = "{\"valid\":true}";
        }
        //response把jsonResult打到前台
        response.getWriter().write(jsonResult);

    }

    private void checkRoomNameIsExits(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String roomname = request.getParameter("room_name");

        if (roomname != null) {
            //服务层service调用数据库访问层dao中的searchUserName方法。
            ClassService c = new ClassServiceImpl();
            List<Map> map = c.selectClassRoom(roomname);
            if(map.isEmpty()){
                response.getWriter().write("true");
            }else{
                response.getWriter().write("false");
            }

        }
    }
    private void addClassRoom(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String roomname = request.getParameter("room_name");
        int result=-1;
        if (roomname!= null) {
            //服务层service调用数据库访问层dao中的searchUserName方法。
            ClassService c = new ClassServiceImpl();
            result = c.addClassRoom(roomname);
            if(result==1){
                response.getWriter().write("ok");
                System.out.println(result);
            }else{
                response.getWriter().write("error");
                System.out.println(result);
            }

        }
    }
    private void queryOneClass(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String classid = request.getParameter("classid");
        ClassService c=new ClassServiceImpl();
        List<Map> map=c.queryOneClass(Integer.parseInt(classid));
        String tr=JSON.toJSONStringWithDateFormat(map,"YYYY-mm-dd");
        response.getWriter().print(tr);
    }

    private void delete_class(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String classid = request.getParameter("classid");
        ClassService c=new ClassServiceImpl();
        int result=-1;
        PrintWriter writer = response.getWriter();
        result=c.delete_class(Integer.parseInt(classid));
        if(result!=-1){
            writer.print("ok");
        }else{
            writer.print("error");
        }

    }

}


